Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix how we cancel the context in the builtin backup engine #17285

Merged
merged 2 commits into from
Nov 27, 2024

Conversation

frouioui
Copy link
Member

@frouioui frouioui commented Nov 26, 2024

Description

This PR makes sure we cancel the context only at the correct time. It was getting canceled too early which could lead to incomplete/failing backups with storage implementations that write concurrently (Ceph and S3).

This regression was introduced by: #16856, backporting this to release-21.0 as the issue was introduced there. It would lead to the following error:

commerce/0 (zone1-0000000102): time:{seconds:1732657021 nanoseconds:695573000} file:"builtinbackupengine.go" line:509 value:"resetting mysqld super_read_only to true"
commerce/0 (zone1-0000000102): time:{seconds:1732657021 nanoseconds:696651000} file:"builtinbackupengine.go" line:526 value:"restarting mysql replication"
E1126 15:37:02.757871   98619 main.go:56] rpc error: code = Unknown desc = TabletManager.Backup on zone1-0000000102: operation error S3: PutObject, https response error StatusCode: 0, RequestID: , HostID: , canceled, context canceled;operation error S3: PutObject, https response error StatusCode: 0, RequestID: , HostID: , canceled, context canceled;operation error S3: PutObject

Copy link
Contributor

vitess-bot bot commented Nov 26, 2024

Review Checklist

Hello reviewers! 👋 Please follow this checklist when reviewing this Pull Request.

General

  • Ensure that the Pull Request has a descriptive title.
  • Ensure there is a link to an issue (except for internal cleanup and flaky test fixes), new features should have an RFC that documents use cases and test cases.

Tests

  • Bug fixes should have at least one unit or end-to-end test, enhancement and new features should have a sufficient number of tests.

Documentation

  • Apply the release notes (needs details) label if users need to know about this change.
  • New features should be documented.
  • There should be some code comments as to why things are implemented the way they are.
  • There should be a comment at the top of each new or modified test to explain what the test does.

New flags

  • Is this flag really necessary?
  • Flag names must be clear and intuitive, use dashes (-), and have a clear help text.

If a workflow is added or modified:

  • Each item in Jobs should be named in order to mark it as required.
  • If the workflow needs to be marked as required, the maintainer team must be notified.

Backward compatibility

  • Protobuf changes should be wire-compatible.
  • Changes to _vt tables and RPCs need to be backward compatible.
  • RPC changes should be compatible with vitess-operator
  • If a flag is removed, then it should also be removed from vitess-operator and arewefastyet, if used there.
  • vtctl command output order should be stable and awk-able.

@vitess-bot vitess-bot bot added NeedsBackportReason If backport labels have been applied to a PR, a justification is required NeedsDescriptionUpdate The description is not clear or comprehensive enough, and needs work NeedsIssue A linked issue is missing for this Pull Request NeedsWebsiteDocsUpdate What it says labels Nov 26, 2024
@github-actions github-actions bot added this to the v22.0.0 milestone Nov 26, 2024
@frouioui frouioui removed NeedsDescriptionUpdate The description is not clear or comprehensive enough, and needs work NeedsWebsiteDocsUpdate What it says NeedsIssue A linked issue is missing for this Pull Request NeedsBackportReason If backport labels have been applied to a PR, a justification is required labels Nov 26, 2024
Copy link

codecov bot commented Nov 26, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 67.39%. Comparing base (8648264) to head (4cc10e5).
Report is 2 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #17285      +/-   ##
==========================================
- Coverage   67.40%   67.39%   -0.02%     
==========================================
  Files        1574     1574              
  Lines      253205   253217      +12     
==========================================
- Hits       170676   170643      -33     
- Misses      82529    82574      +45     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Contributor

@shlomi-noach shlomi-noach left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please see inline question - I unfortunately do not follow the logic.

if finalErr != nil {
cancel()
}
}()
Copy link
Contributor

@shlomi-noach shlomi-noach Nov 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure I follow:

  • It looks as if the function can exit and still leave the context active, in which case, what is cancelling it?
  • If we still have operations in flight, why would we exit the function? Should we not wait until everything is complete?

Something feels off here, as an anti-pattern.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The S3 and Ceph uploads may be incomplete by the time we return from this function. Writing to the buffers used by these storage implementation will be complete (which is how we're able to return from be.backupFile and be.backupFiles), but the actual reading from the buffer and uploading to the remote storage will/may not be complete. If we cancel the context too early, unfinished uploads will be canceled and marked as failed.

It is only at a later stage where we wait for all the uploads to be finished with the EndBackup method on the backup handle:

case BackupUsable:
finishErr = bh.EndBackup(ctx)

At this stage we will observe the failures created by the S3 or Ceph storage implementation and will decide to fail - even though we have already uploaded the backup (including the MANIFEST) and restarted MySQL.

There is definitely something off with this code. We must wait for the full backup (writing to the backend storage included) to complete before going forward with writing the MANIFEST and assuming the backup is useable. This is something that I am implementing along with a retry mechanism on: #17271.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what is cancelling it?

The caller of ExecuteBackup will eventually cancel it, whether it is through a gRPC call or through vtbackup, the context always gets canceled.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the clarity! ❤️

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@frouioui How can it ever cancel it? ctx might be cancelled, but ctxCancel is not? That leads to a memory leak.

So even if the outer context is cancelled, we still need to cancel this inner one to avoid the memory leak.

@frouioui frouioui merged commit 0726ea6 into vitessio:main Nov 27, 2024
104 of 112 checks passed
@frouioui frouioui deleted the fix-context-cancel-builtin branch November 27, 2024 19:58
vitess-bot pushed a commit that referenced this pull request Nov 27, 2024
deepthi pushed a commit that referenced this pull request Nov 28, 2024
…gine (#17285) (#17291)

Signed-off-by: Florent Poinsard <[email protected]>
Co-authored-by: vitess-bot[bot] <108069721+vitess-bot[bot]@users.noreply.github.com>
@frouioui frouioui mentioned this pull request Dec 2, 2024
5 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants